//
//  CheckBigImgView.m
//  Elife
//
//  Created by 拓客营 on 2022/4/16.
//

#import "CheckBigImgView.h"
#import <UIImageView+WebCache.h>

@interface CheckBigImgView()

@property (nonatomic, assign) UIView *background;//图片放大
@property (nonatomic , strong) UIImageView *browseImgView;

@end

@implementation CheckBigImgView

+(void)imageWithImage:(UIImage *)img {
    CheckBigImgView *view = [[CheckBigImgView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) image:img IndexString:nil];
    [kWindow addSubview:view];
}

+(void)imageWithURL:(NSString *)urlStr {
    CheckBigImgView *view = [[CheckBigImgView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) image:nil IndexString:urlStr];
    [kWindow addSubview:view];
}

- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)Selectimage IndexString:(NSString *)IndexString
{
    if (self = [super initWithFrame:frame])
    {
        //创建一个黑色背景
        //初始化一个用来当做背景的View。
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _background = bgView;
        [bgView setBackgroundColor:[UIColor blackColor]];
        [self addSubview:bgView];
        
        //创建显示图像的视图
        //初始化要显示的图片内容的imageView
        UIImageView *browseImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight - 20)];
        browseImgView.contentMode = UIViewContentModeScaleAspectFit;
        self.browseImgView = browseImgView;
        //要显示的图片，即要放大的图片
        if (Selectimage) {
            _browseImgView.image = Selectimage;
        }
        //如果传递的是url
        if (IndexString) {
            [_browseImgView sd_setImageWithURL:[NSURL URLWithString:IndexString] placeholderImage:nil];
        }
        
    
        [bgView addSubview:browseImgView];
        
        browseImgView.userInteractionEnabled = YES;
        //添加点击手势（即点击图片后退出全屏）
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
        [browseImgView addGestureRecognizer:tapGesture];
        
        [self shakeToShow:bgView];//放大过程中的动画
    }
    return self;
}
-(void)closeView
{
    [self removeFromSuperview];
}
//放大过程中出现的缓慢动画
- (void) shakeToShow:(UIView*)aView
{
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.15;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [aView.layer addAnimation:animation forKey:nil];
}

@end
